package com.five.calendar.database.observer;

import com.five.calendar.common.observer.Observable;
import com.five.calendar.database.DataProvider;
import com.five.calendar.database.bean.ScheduleBean;

import java.util.List;

/**
 * Created by xin on 2017/8/29.
 */

public class ScheduleObservable extends Observable<ScheduleBean, Integer, List<ScheduleBean>>{
    private static final int FLAG_SAVE_SCHEDULE = 1;

    private static final int FLAG_DELETE_SCHEDULE= 2;

    private static final int FLAG_QUERY_SCHEDULE = 3;

    private static final int FLAG_UPDATE_SCHEDULE = 4;

    private static final int FLAG_QUERY_SCHEDULE_BY_TIME = 5;

    private int mFlag;

    private String mTitle;

    private String mAddress;

    private Long mStartTime;

    private Long mEndTime;

    private int mImportance;

    private String mNote;

    private int isRemind;

    private Long mRemindTime;

    private ScheduleBean mScheduleBean;

    private List<ScheduleBean> mList;

    private Long mBegin;

    private Long mEnd;

    public ScheduleObservable(int mFlag){
        this.mFlag = mFlag;
    }

    public ScheduleObservable(int mFlag, ScheduleBean mScheduleBean){
        this.mFlag = mFlag;
        this.mScheduleBean = mScheduleBean;
    }

    private ScheduleObservable(int mFlag, List<ScheduleBean> mList){
        this.mFlag = mFlag;
        this.mList = mList;
    }

    private ScheduleObservable(int mFlag, String mTitle, String mAddress, Long mStartTime, Long mEndTime, int mImportance, String mNote, int isRemind, Long mRemindTime){
        this.mFlag = mFlag;
        this.mTitle = mTitle;
        this.mAddress = mAddress;
        this.mStartTime = mStartTime;
        this.mEndTime = mEndTime;
        this.mImportance = mImportance;
        this.mNote = mNote;
        this.isRemind = isRemind;
        this.mRemindTime = mRemindTime;
    }

    public ScheduleObservable(int mFlag, long begin, long end) {
        this.mFlag = mFlag;
        this.mBegin = begin;
        this.mEnd = end;
    }

    public static ScheduleObservable querySchedule() {
        return new ScheduleObservable(FLAG_QUERY_SCHEDULE);
    }

    public static ScheduleObservable queryScheduleByTime(long begin, long end) {
        return new ScheduleObservable(FLAG_QUERY_SCHEDULE_BY_TIME, begin, end);
    }
    public static ScheduleObservable deleteSchedule(List<ScheduleBean> mList) {
        return new ScheduleObservable(FLAG_DELETE_SCHEDULE, mList);
    }

    public static ScheduleObservable updateSchedule(ScheduleBean mHistoryBean) {
        return new ScheduleObservable(FLAG_UPDATE_SCHEDULE, mHistoryBean);
    }

    public static ScheduleObservable saveSchedule(String mTitle, String mAddress, Long mStartTime, Long mEndTime, int mImportance, String mNote, int isRemind, Long mRemindTime) {
        return new ScheduleObservable(FLAG_SAVE_SCHEDULE, mTitle, mAddress, mStartTime, mEndTime, mImportance, mNote, isRemind, mRemindTime);
    }

    @Override
    public void run() {
        switch (mFlag) {
            case FLAG_SAVE_SCHEDULE:
                ScheduleBean scheduleBean = DataProvider.getInstance().insert(mTitle, mAddress, mStartTime, mEndTime, mImportance, mNote, isRemind, mRemindTime);
                if (null != scheduleBean) {
                    handleSuccess(scheduleBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
            case FLAG_DELETE_SCHEDULE:
                if (DataProvider.getInstance().delete(mList)) {
                    for (ScheduleBean bean : mList) {
                        handleSuccess(bean);
                    }
                } else {
                    handleFail(-1);
                }
                handleComplete(mList);
                break;
            case FLAG_QUERY_SCHEDULE:
                handleComplete(DataProvider.getInstance().query());
                break;
            case FLAG_UPDATE_SCHEDULE:
                if (DataProvider.getInstance().update(mScheduleBean)) {
                    handleSuccess(mScheduleBean);
                } else {
                    handleFail(-1);
                }
                handleComplete(null);
                break;
            case FLAG_QUERY_SCHEDULE_BY_TIME:
                handleComplete(DataProvider.getInstance().queryByTime(mBegin, mEnd));
                break;
        }
    }
}
